package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.util.ImageLoader;

import java.util.List;

public class RecyclerHomeAdapter extends RecyclerView.Adapter {

    private Context mContext; // Activity
    private List<Goods> goodsList; // 数据
    private RecyclerHomeAdapter.OnItemClickListener mOnItemClickListener; // 定义一个监听接口


    public interface OnItemClickListener {  // 定义类型
        void onItemClick(View view, int position);  // 需要重写
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mOnItemClickListener = listener;
    }


    // 构造方法
    public RecyclerHomeAdapter(Context mContext, List<Goods>goodsList) {
        this.mContext = mContext;
        this.goodsList = goodsList;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_goods,parent, false);
        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder vh, int position) {
        // 根据位置绑定数据
        ItemHolder holder = (ItemHolder) vh;
        Goods data = goodsList.get(position);   // 当前位置的数据
//        holder.iv_thumb.setImageResource(data.getPicRes());
        // 使用 Glide 加载本地图片
        Glide.get(mContext).clearMemory();
        ImageLoader.loadImage(mContext, data.getPicPath(), holder.iv_thumb);
        // 列表项的点击事件需要自己实现
        holder.ll_item.setOnClickListener(view -> {
            if(mOnItemClickListener != null){   // 调用 mOnItemClickListener的方法
                mOnItemClickListener.onItemClick(view,position);
            }
        });
        holder.tv_desc.setText(data.getDesc());
        holder.tv_desc.setMaxLines(1);
        holder.tv_price.setText("￥"+data.getPrice());
        holder.tv_capcity.setText("库存:"+data.getCapcity());
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }


    public class ItemHolder extends RecyclerView.ViewHolder{
        private LinearLayout ll_item;
        private ImageView iv_thumb;
        private TextView tv_desc;
        private TextView tv_price;
        private TextView tv_capcity;

        public ItemHolder(View itemView) {
            super(itemView);
            ll_item = itemView.findViewById(R.id.ll_item);
            iv_thumb = itemView.findViewById(R.id.iv_thumb);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_capcity = itemView.findViewById(R.id.tv_capcity);
        }


    }
}
